package top.sdwu_top.kbase.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.google.common.collect.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import top.sdwu_top.kbase.entity.KbaseFileTag;
import top.sdwu_top.kbase.mapper.KbaseFileTagMapper;
import top.sdwu_top.kbase.service.IKbaseFileTagService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import top.sdwu_top.kbase.service.IKbaseKnowledgeTagService;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 文件标签表 服务实现类
 * </p>
 *
 * @author zhipeng.duan@nvr-china.com
 * @since 2020-02-20
 */
@Service
public class KbaseFileTagServiceImpl extends ServiceImpl<KbaseFileTagMapper, KbaseFileTag> implements IKbaseFileTagService {

    @Autowired
    IKbaseKnowledgeTagService tagService;

    @Override
    public boolean save(KbaseFileTag fileTag) {
        return super.save(fileTag);
    }

    @Override
    public KbaseFileTag update(KbaseFileTag fileTag) {
        return null;
    }

    @Override
    public List<String> getTagIdListByFileId(String id) {
        QueryWrapper<KbaseFileTag> qw = Wrappers.query();
        qw.select("TAG_ID");
        qw.eq("FILE_ID", id);
        List<KbaseFileTag> fileTagList = super.list(qw);
        if (CollectionUtils.isEmpty(fileTagList)) {
            return Lists.newArrayListWithExpectedSize(0);
        }
        return fileTagList.parallelStream().map(KbaseFileTag::getTagId).collect(Collectors.toList());
    }

    private boolean exist(KbaseFileTag fileTag) {
        QueryWrapper<KbaseFileTag> qw = Wrappers.query();
        return false;
    }

    @Override
    public List<String> getFileIdListByTagKeyword(String keyword) {
        List<String> tagIdList = tagService.getIdListByKeyword(keyword);
        if (CollectionUtils.isEmpty(tagIdList)) {
            return Lists.newArrayListWithExpectedSize(0);
        }
        QueryWrapper<KbaseFileTag> qw = Wrappers.query();
        qw.in("TAG_ID", tagIdList);
        List<KbaseFileTag> fileTagList = super.list(qw);
        if (CollectionUtils.isEmpty(fileTagList)) {
            return Lists.newArrayListWithExpectedSize(0);
        }
        return fileTagList.parallelStream().map(KbaseFileTag::getFileId).collect(Collectors.toList());
    }
}
